package org.cy.websocket.stomp.client;

import org.cy.websocket.stomp.StompClient;
import org.cy.websocket.stomp.StompHeaders;
import org.cy.websocket.stomp.StompSubscription;
import org.cy.websocket.stomp.messaging.StringMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created in 2021-05-13 14:31
 *
 * @author cao.yong
 */
@RestController
@RequestMapping("/stomp/client")
public class StompClientController {

    private static final Logger LOG = LoggerFactory.getLogger(StompClientController.class);

    private final StompClient client;

    private StompSubscription topicSubscription;
    private StompSubscription singleSubscription;

    public StompClientController(StompClient client) {
        this.client = client;
    }

    @GetMapping("/connect")
    public void connect() {
        this.client.connect(getHeaders());
    }

    @GetMapping("/subscribe")
    public void subscribe() {
        if (null == topicSubscription) {
            topicSubscription = this.client.subscribe("/topic/broadcast", getHeaders(), response -> {
                LOG.info("/topic/msg: ");
                LOG.info("command: {}", response.getCommand());
                LOG.info("headers: {}", response.getHeaders().toString());
                LOG.info("payload: {}", StringMessageConverter.fromMessage(response));
            });
        }
        if (null == singleSubscription) {
            StompHeaders headers = getHeaders();
            String id = headers.getFirst("deviceName");
            singleSubscription = this.client.subscribe("/user/" + id + "/msg", getHeaders(), response -> {
                LOG.info("/user/{}/receive response: ", id);
                LOG.info("command: {}", response.getCommand());
                LOG.info("headers: {}", response.getHeaders().toString());
                LOG.info("payload: {}", StringMessageConverter.fromMessage(response));
            });
        }
    }

    @GetMapping("/send")
    public void sendTopic() {
        this.client.send("/app/broadcast/msg", getHeaders(), "Send Topic Message");
        this.client.send("/app/single/msg", getHeaders(), "Send Single Message");
    }

    @GetMapping("/unsubscribe")
    public void unsubscribe() {
        if (null != topicSubscription) {
            topicSubscription.unsubscribe(getHeaders());
            topicSubscription = null;
        }
        if (null != singleSubscription) {
            singleSubscription.unsubscribe(getHeaders());
            singleSubscription = null;
        }
    }

    @GetMapping("/disconnect")
    public void disconnect() {
        this.client.disconnect(getHeaders());
    }

    private StompHeaders getHeaders() {
        StompHeaders headers = new StompHeaders();
        headers.add("deviceName", "01");
        headers.add("deviceType", "0");
        headers.add("deviceInit", "true");
        headers.add("devicePair", "");
        return headers;
    }
}
